package com.chalk.service.impl;

import com.chalk.service.WxMiniPayService;
import com.chalk.util.JAXBUtil;
import com.chalk.util.WxUtil;
import com.chalk.util.wxminipay.WxMiniPayConfig;
import com.chalk.util.wxminipay.WxMiniPayResultSuccess;
import com.chalk.util.wxminipay.WxMiniQueryResult;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Service
public class WxMiniPayServiceImpl implements WxMiniPayService {


    @Override
    public WxMiniPayResultSuccess generateWxOrder(String body, String outTradeNo, Integer total_fee,String openid) {
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        // DigestUtils.md5Hex()
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        // MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        WxMiniPayConfig wxPayParam = new WxMiniPayConfig();

        wxPayParam.setSpbill_create_ip("139.9.48.110");  //部署时设置服务端ip
        wxPayParam.setBody(body);
        wxPayParam.setNonce_str(RandomStringUtils.random(32, true, true));
        System.out.println("-----订单编号----" + outTradeNo);
        wxPayParam.setOut_trade_no(outTradeNo);
        wxPayParam.setTotal_fee(total_fee);
        wxPayParam.setOpen_id(openid);

        wxPayParam.setSign(WxUtil.generatorPaySignForMD5(wxPayParam));


        StringBuffer xmlString = new StringBuffer("");

        xmlString.append("<xml>\n");
        xmlString.append("<appid>" + WxMiniPayConfig.appid + "</appid>\n");
        xmlString.append("<body>" + wxPayParam.getBody() + "</body>\n");
        xmlString.append("<mch_id>" + WxMiniPayConfig.mch_id + "</mch_id>\n");
        xmlString.append("<nonce_str>" + wxPayParam.getNonce_str() + "</nonce_str>\n");
        xmlString.append("<notify_url>" + WxMiniPayConfig.notify_url + "</notify_url>\n");
        xmlString.append("<openid>" + wxPayParam.getOpen_id() + "</openid>\n");
        xmlString.append("<out_trade_no>" + wxPayParam.getOut_trade_no() + "</out_trade_no>\n");
        xmlString.append("<spbill_create_ip>" + wxPayParam.getSpbill_create_ip() + "</spbill_create_ip>\n");
        xmlString.append("<total_fee>" + wxPayParam.getTotal_fee() + "</total_fee>\n");
        xmlString.append("<trade_type>" + WxMiniPayConfig.trade_type + "</trade_type>\n");
        xmlString.append("<sign>" + wxPayParam.getSign() + "</sign>\n");

        xmlString.append("</xml>");
//        map.add("appid",WxPayParam.appid);
//        map.add("body",wxPayParam.getBody());
//        map.add("mch_id",WxPayParam.mch_id);
//        map.add("nonce_str",wxPayParam.getNonce_str());
//        map.add("notify_url",WxPayParam.notify_url);
//        map.add("out_trade_no",wxPayParam.getOut_trade_no());
//        map.add("spbill_create_ip",wxPayParam.getSpbill_create_ip());
//        map.add("total_fee",wxPayParam.getTotal_fee());
//        map.add("trade_type",WxPayParam.trade_type);
//        map.add("sign",wxPayParam.getSign());
//请求体

        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        HttpEntity<String> formEntity = new HttpEntity<>(xmlString.toString(), headers);
        ResponseEntity<String> entity = restTemplate.postForEntity(url, formEntity, String.class);


        System.out.println("小程序支付返回数据：" + entity.getBody());

        System.out.println();
        return JAXBUtil.convertToJavaBean(WxMiniPayResultSuccess.class, entity.getBody());
    }

    @Override
    public WxMiniQueryResult queryWxOrder(String outTradeNo) {
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        // MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        WxMiniPayConfig wxPayParam = new WxMiniPayConfig();

        wxPayParam.setOut_trade_no(outTradeNo);
        wxPayParam.setNonce_str(RandomStringUtils.random(32, true, true));

        wxPayParam.setSign(WxUtil.generatorQueryForMD5(wxPayParam));

        StringBuffer xmlString = new StringBuffer("");

        xmlString.append("<xml>\n");
        xmlString.append("<appid>" + WxMiniPayConfig.appid + "</appid>\n");
        xmlString.append("<mch_id>" + WxMiniPayConfig.mch_id + "</mch_id>\n");
        xmlString.append("<nonce_str>" + wxPayParam.getNonce_str() + "</nonce_str>\n");
        xmlString.append("<out_trade_no>" + wxPayParam.getOut_trade_no() + "</out_trade_no>\n");
        xmlString.append("<sign>" + wxPayParam.getSign() + "</sign>\n");
        xmlString.append("</xml>");

        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        HttpEntity<String> formEntity = new HttpEntity<>(xmlString.toString(), headers);
        ResponseEntity<String> entity = restTemplate.postForEntity(url, formEntity, String.class);



        System.out.println("小程序支付返回数据：" + entity.getBody());
        return  JAXBUtil.convertToJavaBean(WxMiniQueryResult.class, entity.getBody());
    }
}
